﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Nb.Common.Aop
{
    public class PerformanceTracer
    {
        public static void LogMessageIfNecessary(TimeSpan timeSpan, string message, string messagePrefix = "[PerformanceTracer]", char star = '☆', int importantSpan = 100)
        {
            var startMessage = MakeStartMessage(timeSpan, message, star, importantSpan);
            if (!startMessage.StartsWith("OK"))
            {
                LogMessage(startMessage, messagePrefix);
            }
        }
        private static string MakeStartMessage(TimeSpan timeSpan, string message, char star, int importantSpan)
        {
            string theStar = null;
            int importantCount = (int)timeSpan.TotalMilliseconds / importantSpan;
            if (importantCount < 1)
            {
                theStar = "OK";
            }
            else
            {
                theStar = "Slow:";
                for (int i = 0; i < importantCount; i++)
                {
                    if (i > 10)
                    {
                        theStar = "Very Very " + theStar;
                        break;
                    }
                    if (i > 4)
                    {
                        theStar = "Very " + theStar;
                        break;
                    }
                    theStar += star;
                }
            }

            return string.Format("{0} {1}", theStar, message);
        }
        private static void LogMessage(string message, string messagePrefix)
        {
            UtilsLogger.LogMessage(messagePrefix + message);
        }
    }
}
